﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormProject.Depo
{
    public class GenelIslemler
    {
        DataBaseProvider MyDataBase = new DataBaseProvider();
        public int DonusturToInt32(string textbox)
        {
            try
            {
                if (textbox == null || textbox == "")
                    return 0;
                else
                    return Convert.ToInt32(textbox);
            }
            catch
            {
                return 0;
            }
        }

        public DataTable Kategoriler()
        {
            DataTable ReturnValue;
            ReturnValue = MyDataBase.ExecuteDataTable("Select * from Kategoriler", CommandType.Text,"Kategoriler");         
            return ReturnValue;

 
        }
        public DataTable Subeler()
        {
            DataTable ReturnValue;
            ReturnValue = MyDataBase.ExecuteDataTable("Select * from Subeler", CommandType.Text);
            return ReturnValue;


        }
        public static void BackGrounWorkerCalistir(BackgroundWorker BGW)
        {

            if (BGW.IsBusy != true)
            {
                BGW.RunWorkerAsync();
            }
            else
            {
                BGW.CancelAsync();

            }

        }

        public static void TextboxBindingEt(BindingSource BindingSource, Control.ControlCollection MyController)
        {
            if (BindingSource != null)
            {
                for (int i = 0; i < MyController.Count; i++)
                {
                    if (MyController[i].DataBindings.Count > 0)
                    {
                        MyController[i].DataBindings.Clear();

                    }
                }

                for (int i = 0; i < MyController.Count; i++)
                {

                    if ((MyController[i] is TextBox || MyController[i] is MaskedTextBox || MyController[i] is ComboBox))
                    {
                        MyController[i].DataBindings.Add(new Binding("Text", BindingSource, MyController[i].Name));
                    }



                }
            }
        }

        public static int BosSatirVarmi(BindingSource MySource)
        {
            MySource.RemoveFilter();
            int returnvalue = -1;
            for (int i = 0; i < MySource.List.Count; i++)
            {
                DataRowView DRW = ((DataRowView)(MySource.List[i]));
                if (DRW.Row.RowState == DataRowState.Added)
                {
                    returnvalue = i;
                    break;
                }
            }


            return returnvalue;
        }
        public static void SatirGirisiKisitla(DataGridViewCell KontrolEdilecekHucre, DataGridViewRow DigerHucreler)
        {
            if (KontrolEdilecekHucre.Value != DBNull.Value)
            {

                foreach (DataGridViewCell Cell in DigerHucreler.Cells)
                {
                    Cell.ReadOnly = false;
                }

            }
            else if (KontrolEdilecekHucre.Value == DBNull.Value)
            {
                foreach (DataGridViewCell Cell in DigerHucreler.Cells)
                {
                    if (Cell == KontrolEdilecekHucre)
                    {
                        Cell.ReadOnly = false;
                    }
                    else
                    {
                        Cell.ReadOnly = true;
                    }

                }

            }
        }
        public static void KontrolAktifPasif(DataGridViewCell KontrolEdilecekHucre, Control.ControlCollection Kontroller)
        {
            if (KontrolEdilecekHucre.Value.ToString() != string.Empty)
            {

                foreach (Control Ctrl in Kontroller)
                {
                    Ctrl.Enabled = true;
                }

            }
            else if (KontrolEdilecekHucre.Value.ToString() == string.Empty)
            {
                foreach (Control Ctrl in Kontroller)
                {

                    Ctrl.Enabled = false;
                }

            }
        }
        public static bool HucreIcerigiBosmu(DataGridViewCell MyCell)
        {
            bool ReturnValue = false;
            if (MyCell.Value.ToString() != string.Empty)
            {
                ReturnValue = true;
            }
            else
            {
                ReturnValue = false;
            }
            return ReturnValue;

        }
        public static void ToolstripMesajYolla(ToolStripStatusLabel Label, string Mesaj)
        {
            Label.Text = Mesaj;

        }
        public static void ToolstripMesajYolla(Form Frm, string Mesaj)
        {
            //((EtpMenu)(Frm)).toolStripwindowStatuslbl.Visible = true;
            ((MainForm )(Frm)).toolStripMesaj.Text = Mesaj;
            ((MainForm )(Frm)).timer2.Enabled = true;
        }
        public static void Enterileilerle(string frmName, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
                Application.OpenForms[frmName].SelectNextControl(Application.OpenForms[frmName].ActiveControl, true, true, true, true);
        }

        public static void EnterIlerleload(string frmname)
        {
            foreach (Control item in Application.OpenForms[frmname].Controls)
            {
                if (item is TextBox || item is ComboBox || item is CheckBox || item is MaskedTextBox || item is Button || item is DateTimePicker)
                {
                    item.KeyDown += new KeyEventHandler(item_KeyDown);
                }
            }
        }

        static void item_KeyDown(object sender, KeyEventArgs e)
        {
            Depo.GenelIslemler.Enterileilerle((sender as Control).Parent.Name, e);
        }

      
    }
}
